import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * La clase Devices agrupa a los datos informativos sobre
 * el número y estado de dispositivos presentes en el sistema.
 */
public class Devices extends DataCategory
{
    public Devices()
    {
    }
    public boolean initData(int id,int value)
    {
        
        if(id <= 0x0F)
            switch(id)
            {
                case KbdCmdId.CmdId.DEVICE:
                    if(value < 0x10)
                        lightStatus = Integer.valueOf(value);
                    else
                        coolerStatus = Integer.valueOf(value-0x10);
                    return true;
                default:
                    return false;
            }
        else
            switch(id)
            {
                case NUMBER_OF_DEVICES:
                    numberDevices = Integer.valueOf(value);
                    return true;
                default:
                    return false;
            }
    }
    public boolean initData(int id,long value)
    {
        return initData( id,(int) value);
    }
    public Component getView(Id viewID)
    {
        switch(viewID)
        {
            case DEVICES:
                JPanel panel = new JPanel();
                JPanel panel_aux1 = new JPanel();
                JPanel panel_aux2 = new JPanel();
                panel.setLayout(new GridLayout(2,0));
                panel_aux1.setLayout(new BoxLayout(panel_aux1,BoxLayout.X_AXIS));
                panel_aux2.setLayout(new BoxLayout(panel_aux2,BoxLayout.X_AXIS));
                JLabel lightLabel = NewComp.getLabel("light", "titleLabelTFrame", "blue");
                lightIcon.setIcon(NewComp.getIcon(deviceOpt[lightStatus]));
                panel_aux1.add(lightLabel);
                panel_aux1.add(lightIcon);
                JLabel coolerLabel = NewComp.getLabel("cooler", "titleLabelTFrame", "blue");
                coolerIcon.setIcon(NewComp.getIcon(deviceOpt[coolerStatus]));
                panel_aux2.add(coolerLabel);
                panel_aux2.add(coolerIcon);
                panel.add(panel_aux1);
                panel.add(panel_aux2);
                return panel;
            default:
                return null;
        }
    }
    public Component getEditor(Id editorID)
    {
        JPanel panel = new JPanel();
        ButtonGroup groupLS = new ButtonGroup();
        JPanel panelLS = new JPanel(new GridLayout(1,3));
        ButtonGroup groupCS = new ButtonGroup();
        JPanel panelCS = new JPanel(new GridLayout(1,3));
        switch(editorID)
        {
            case DEVICES:
                panel.setLayout(new GridLayout(1,2));
                JPanel panel_Aux1 = NewComp.getPanel("light","titleBorderBFrame",new GridLayout(2,1));
                JLabel lightSLabel = NewComp.getLabel("status","titleLabelBFrame","black");
                JRadioButton lightSOn =NewComp.getRadioButton( 
                    "on", 
                    "titleRButton", 
                    "blue",
                    deviceOpt[lightStatus].equals("on"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.DEVICE, KbdCmdId.CmdValue.DEVICE_ENABLE);
                        }
                    }
                );
                JRadioButton lightSOff =NewComp.getRadioButton( 
                    "off", 
                    "titleRButton",
                    "blue",
                    deviceOpt[lightStatus].equals("off"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(
                                KbdCmdId.CmdId.DEVICE, 
                                KbdCmdId.CmdValue.DEVICE_DISABLE
                            );
                        }
                    }
                );
                JRadioButton lightSAuto =NewComp.getRadioButton( 
                    "auto", 
                    "titleRButton",
                    "blue",
                    deviceOpt[lightStatus].equals("auto"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.DEVICE, KbdCmdId.CmdValue.DEVICE_AUTOMATIC);
                        }
                    }
                );
                groupLS.add(lightSOn);
                groupLS.add(lightSOff);
                groupLS.add(lightSAuto);
                panelLS.add(lightSOn);
                panelLS.add(lightSOff);
                panelLS.add(lightSAuto);
                
                JPanel panel_Aux2 = NewComp.getPanel("cooler","titleBorderBFrame",new GridLayout(2,1));
                JLabel coolerSLabel = NewComp.getLabel("status","titleLabelBFrame","black");
                JRadioButton coolerSOn =NewComp.getRadioButton( 
                    "on", 
                    "titleRButton",
                    "blue",
                    deviceOpt[coolerStatus].equals("on"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(
                                KbdCmdId.CmdId.DEVICE, 
                                KbdCmdId.CmdValue.DEVICE_ENABLE+0x10);
                        }
                    }
                );
                JRadioButton coolerSOff =NewComp.getRadioButton( 
                    "off", 
                    "titleRButton",
                    "blue",
                    deviceOpt[coolerStatus].equals("off"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(
                                KbdCmdId.CmdId.DEVICE, 
                                KbdCmdId.CmdValue.DEVICE_DISABLE+0x10);
                        }
                    }
                );
                JRadioButton coolerSAuto =NewComp.getRadioButton( 
                    "auto", 
                    "titleRButton",
                    "blue",
                    deviceOpt[coolerStatus].equals("auto"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(
                                KbdCmdId.CmdId.DEVICE, 
                                KbdCmdId.CmdValue.DEVICE_AUTOMATIC+0x10);
                        }
                    }
                );
                groupCS.add(coolerSOn);
                groupCS.add(coolerSOff);
                groupCS.add(coolerSAuto);
                panelCS.add(coolerSOn);
                panelCS.add(coolerSOff);
                panelCS.add(coolerSAuto);
                
                panel_Aux1.add(lightSLabel);
                panel_Aux1.add(panelLS);
                panel_Aux2.add(coolerSLabel);
                panel_Aux2.add(panelCS);
                panel.add(panel_Aux1);
                panel.add(panel_Aux2);
                return panel;
            default:
                return null;
        }
    }
    public String getDataCategoryID()
    {
        return "devices";
    }
    public String getValue(Id dataID)
    {
        return null;
    }
    public boolean setCommandValue(int id,int value)
    {
        initData(id,value);
        if(id <= 0x0F)
            switch(id)
            {
                case KbdCmdId.CmdId.DEVICE:
                    if(value < 0x10)
                        lightIcon.setIcon(NewComp.getIcon(deviceOpt[lightStatus]));
                    else
                        coolerIcon.setIcon(NewComp.getIcon(deviceOpt[coolerStatus]));
                    return true;
                default:
                    return false;
            }
        else
            return false;
    }
    public boolean setCommandValue(int id,long value)
    {
        return setCommandValue( id,(int) value);
    }
    public boolean setDataValue(int id,int value)
    {
        return false;
    }
    public boolean setDataValue(int id,long value)
    {
        return false;
    }
    public void applyChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
    }
    public void cancelChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case OTHER:
                    break;
                default:
                    break;
            }
        }
        changed.clear();
    }
    static
    {
        numberDevices = new Integer(2);
        lightStatus = new Integer(1);
        coolerStatus = new Integer(1);
        lightIcon = NewComp.getLabel();
        coolerIcon = NewComp.getLabel();
    }
    private static final String[] deviceOpt = {
        "off",
        "on",
        "auto"
    };
    private static Integer numberDevices;
    private static Integer lightStatus;
    private static Integer coolerStatus;
    private static JLabel lightIcon;
    private static JLabel coolerIcon;
}
